// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import axios from 'axios' // ajax
import qs from 'qs'
import store from './storeLocal' // 数据仓库

import router from './router/index.js'

// 滚动条
import Vuebar from 'vuebar'
Vue.use(Vuebar)

import VueJsonp from 'vue-jsonp'
Vue.use(VueJsonp)

// 本地数据库
import DesignDB from './components/indexedDB/designDB.js'

// 导入字体图标
import './assets/icon-font/iconfont.css'

// 导入全局函数
import Download from './tool/download.js' // 下载
import Browser from './tool/broeser/browser.js' // 判断浏览器类型
import windowOpen from './tool/windowOpen.js' // 新开窗口
import exportData from './tool/export.js' // 导出
import previewFile from './components/appPreview/file.js'
import {getCookie, setCookie} from './tool/cookie.js'
import print from '@/tool/Print.js'

import Moment from 'moment'
import 'moment/locale/zh-cn'
Moment.locale('zh-cn')
import Message from './components/message/index.js' // 警告
import MessageBox from './components/MessageBox/index.js' // 警告(带确定取消按钮)
import accounting from './filter/accounting.js'

axios.defaults.headers['X-Requested-With'] = 'XMLHttpRequest'
axios.defaults.withCredentials = true
import IconColor from './components/appPreview/iconColor.js' // 图标背景色

import SparkMd5 from 'spark-md5'

import OSS from 'ali-oss'
Vue.prototype.$OSS = OSS
// 注册全局函数
Vue.prototype.$message = Message
Vue.prototype.$MessageBox = MessageBox
Vue.prototype.$Browser = Browser
Vue.prototype.$Open = windowOpen
Vue.prototype.$getCookie = getCookie
Vue.prototype.$setCookie = setCookie
Vue.prototype.$IconColor = IconColor
Vue.prototype.$moment = Moment
Vue.prototype.$previewFile = previewFile
Vue.prototype.$qs = qs
Vue.prototype.$SparkMd5 = SparkMd5
Vue.prototype.$DesignDB = DesignDB
Vue.prototype.$Download = Download
Vue.prototype.$export = exportData
Vue.prototype.$print = print
Vue.prototype.$store = store
Vue.prototype.$accounting = accounting

axios.defaults.headers['X-Requested-With'] = 'XMLHttpRequest'
axios.defaults.withCredentials = true
Vue.prototype.$axios = axios
Vue.prototype.$site = JSON.parse(Vue.prototype.$getCookie('server'))

// 注册共用组件
import Select from './components/select/index.js'
import Dialog from './components/dialog/Dialog.vue'
import Row from './components/layout/row/index.js'
import Col from './components/layout/col/index.js'
import Table from './components/table/index.js'
import Search from './components/search/index.js'
import SearchAd from './components/SearchAd/index.js'
import Date from './components/datepicker/index.js'
import GoldLoading from './components/loading/goldLoading.vue'
import Paginate from './components/paginate/index.js'
import Nav from './components/nav/nav.vue'
import User from './components/appUser/user.vue'
import filePreview from './components/appPreview/Preview.vue'
import Radio from './components/radio/radio.vue'
import Check from './components/checked/index.js'
import areaPicker from './components/areaPicker/areaPicker.vue'
import AppBack from './components/back/AppBack.vue'
import fileList from './components/fileList/fileList.vue'
import upload from './components/upload/index.js'
import uploadList from './components/upload/src/FileList.vue'
import uploadImg from './components/upload/src/FileImg.vue'
import Retrieve from './components/retrieve/index.js'
import constructProject from './components/retrieve/src/constructProject/project.vue'
import projectRetrieve from './components/retrieve/src/projectRetrieve.vue'
import Tree from './components/tree/index.js'
// import TreeGrid from './components/treeGrid/index.js'
import AutoComplete from './components/autoComplete/index.js'
import Talk from './components/appTalk/index.js'
import Content from './components/appContent/index.js'
import None from './components/none/none.vue'
import depart from './components/retrieve/src/depart.vue'
import worker from './components/retrieve/src/worker.vue'
import UnderDesign from './components/underDesign/underDesign.vue'
import Switches from './components/switch/switches.vue'
import tab from './components/tab/tab.vue'
import link from './components/link/link.vue'
import ueditor from './components/ueditor/ueditor.vue'
import qriously from './components/qrcode/qriously.vue'

Vue.component('zz-select', Select)
Vue.component('zz-dialog', Dialog)
Vue.component('zz-row', Row)
Vue.component('zz-col', Col)
Vue.component('zz-table', Table)
Vue.component('zz-search', Search)
Vue.component('zz-searchAd', SearchAd)
Vue.component('zz-date', Date)
Vue.component('zz-goldLoading', GoldLoading)
Vue.component('zz-paginate', Paginate)
Vue.component('zz-nav', Nav)
Vue.component('zz-preview', filePreview)
Vue.component('zz-radio', Radio)
Vue.component('zz-check', Check)
Vue.component('zz-areaPicker', areaPicker)
Vue.component('zz-back', AppBack)
Vue.component('zz-fileList', fileList)
Vue.component('zz-upload', upload)
Vue.component('zz-upload-list', uploadList)
Vue.component('zz-upload-img', uploadImg)
Vue.component('zz-retrieve', Retrieve)
Vue.component('zz-depart', depart)
Vue.component('zz-worker', worker)
Vue.component('zz-projectRetrieve', projectRetrieve)
Vue.component('zz-constructProject', constructProject)
Vue.component('zz-tree', Tree)
// Vue.component('zz-treeGrid', TreeGrid)
Vue.component('zz-auto', AutoComplete)
Vue.component('under-design', UnderDesign)
Vue.component('zz-user', User)
Vue.component('zz-talk', Talk)
Vue.component('zz-content', Content)
Vue.component('zz-none', None)
Vue.component('zz-switch', Switches)
Vue.component('zz-tab', tab)
Vue.component('zz-link', link)
Vue.component('zz-ueditor', ueditor)
Vue.component('zz-qriously', qriously)

import draggable from 'vuedraggable'
import {GridLayout, GridItem} from 'vue-grid-layout'
Vue.component('GridLayout', GridLayout)
Vue.component('GridItem', GridItem)
Vue.component('draggable', draggable)

import './tool/requestAnimationFrame'

// 注册全局过滤器
import formatSize from './filter/formatSize.js'
import date from './filter/date.js'
import sex from './filter/sex.js'

Vue.filter('formatSize', formatSize)
Vue.filter('accounting', accounting)
Vue.filter('date', date)
Vue.filter('sex', sex)

import { VTooltip, VPopover } from 'v-tooltip'
Vue.directive('tooltip', VTooltip)
Vue.component('v-popover', VPopover)

// 注册全局指令
import drag from './directives/drag.js'
Vue.directive('drag', drag)

import focus from './directives/focus.js'
Vue.directive('focus', focus)

// 查看视频
import video from './components/video/index'
Vue.use(video)

// 查看图片
import imagePreview from './components/preview/index'
Vue.use(imagePreview)

// 批量导入
import importData from './components/import/index'
Vue.use(importData)

// 导出弹窗
import exportDialog from './components/exportDialog/index'
Vue.use(exportDialog)

// 全局上传组件
import uploadG from './components/upload/global/index'
Vue.use(uploadG)

// 引入BMap
import BaiduMap from 'vue-baidu-map'
Vue.use(BaiduMap, {
  ak: 'br4iwzKIHEoPVqaTWTATRuzM'
})

// 验证
import validationInstall from './components/vaildation/install'
Vue.use(validationInstall)
import validation from './components/vaildation/directive'
Vue.directive('validation', validation)

// 存储请求url
import requestMap from './tool/requestMap.js'
import {SERVER_PINMING_TEST} from './server'
Vue.use(requestMap)

Vue.prototype.$site = SERVER_PINMING_TEST

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  template: '<App/>',
  components: { App }
})

// 添加请求拦截器
axios.interceptors.request.use((config) => {
  // 防止重复请求
  if (Vue.prototype._requestSet.has(config.url) && !config.closeSaveRequest) {
    console.log(`重复请求的URL${config.url}`)
    return
  }
  Vue.prototype._requestSet.add(config.url)

  // 是否关闭加载动画
  if (!config.closeLoading) {
    store.commit('showLoading')
  }
  // 是否打开分页
  if (config.paginate) {
    const queryPaginate = store.getters.getQueryPaginate
    let currentPage = `&query.currentPage=${queryPaginate.currentPage}`
    let pageSize = `&query.pageSize=${queryPaginate.pageSize}`
    // query 是否另外命名
    if (config.queryName) {
      currentPage = `&${config.queryName}.currentPage=${queryPaginate.currentPage}`
      pageSize = `&${config.queryName}.pageSize=${queryPaginate.pageSize}`
    }
    config.data = config.data + currentPage + pageSize
  }
  return config
}, (error) => {
  // 对请求错误做些什么
  return Promise.reject(error)
})

// http响应拦截器
axios.interceptors.response.use(data => {
  Vue.prototype._requestSet.delete(data.config.url)
  if (Vue.prototype._requestSet.size === 0) {
    store.commit('hideLoading')
  }
  // 设置分页参数
  if (data.data.query || data.data.safetyComplaintMsgQuery) {
    store.commit({
      type: 'muPaginate',
      query: data.data.query || data.data.safetyComplaintMsgQuery,
      pagingId: data.config.paginId || data.config.pagingId
    })
  }
  store.commit('removeQueryPaginate')
  return data
}, (error) => {
  if (error.config) {
    Vue.prototype._requestSet.delete(error.config.url)
  }
  store.commit('hideLoading')
  store.commit('removeQueryPaginate')
  return Promise.reject(error)
})
